export function submit(url, data, successCallback, failureCallback,method) {
    showLoading('保存中，请稍候...')
    let submitMethod = method || 'POST'
    $.ajax({
        type : submitMethod,
        url : url,
        data : JSON.stringify(data),
        contentType: "application/json; charset=utf-8", 
        success : function(response) {
            hideLoading()
            let json = response || {}
            if(successCallback) {
                successCallback(json)
            }
        },
        error : function(requestData) {
            hideLoading()
            if(failureCallback) {
                failureCallback(requestData)
            }
        }
    });
}

export function post(url, data, successCallback, failureCallback) {
    showLoading('保存中，请稍候...')
    $.ajax({
        type : 'POST',
        url : url,
        data : JSON.stringify(data),
        contentType: "application/json; charset=utf-8", 
        success : function(response) {
            hideLoading()
            let json = response || {}
            if(successCallback) {
                successCallback(json)
            }
        },
        error : function(requestData) {
            hideLoading()
            if(failureCallback) {
                failureCallback(requestData)
            }
        }
    });
}

export function put(url, data, successCallback, failureCallback) {
    showLoading('保存中，请稍候...')
    $.ajax({
        type : 'PUT',
        url : url,
        data : JSON.stringify(data),
        contentType: "application/json; charset=utf-8", 
        success : function(response) {
            hideLoading()
            let json = response || {}
            if(successCallback) {
                successCallback(json)
            }
        },
        error : function(requestData) {
            hideLoading()
            if(failureCallback) {
                failureCallback(requestData)
            }
        }
    });
}

export function getData(url,data,successCallback, failureCallback) {
    showLoading('数据加载中,请稍候...')
    $.ajax({
        type : 'get',
        url : url,
        data : data,
        success : function(response) {
            hideLoading()
            let json = response || {}
           if(successCallback) {
               successCallback(json)
           }
        },
        error: function(requestData) {
            hideLoading()
            if(failureCallback) {
                failureCallback(requestData)
            }
        }
    });
}

export function del(url,data,successCallback, failureCallback) {
    showLoading('数据加载中,请稍候...')
    $.ajax({
        type : 'DELETE',
        url : url,
        data : data,
        success : function(response) {
            hideLoading()
            let json = response || {}
           if(successCallback) {
               successCallback(json)
           }
        },
        error: function(requestData) {
            hideLoading()
            if(failureCallback) {
                failureCallback(requestData)
            }
        }
    });
}

export function getDataBySync(url,data,successCallback, failureCallback) {
    showLoading('数据加载中,请稍候...')
    $.ajax({
        type : 'get',
        url : url,
        data : data,
        async: false,
        success : function(response) {
            hideLoading()
            let json = response || {}
           if(successCallback) {
               successCallback(json)
           }
        },
        error: function(requestData) {
            hideLoading()
            if(failureCallback) {
                failureCallback(requestData)
            }
        }
    });
}

export function loadJsonDataToForm(form, jsonStr) {
    try {
        var obj = jsonStr;
        var key, value, tagName, type, arr;
        for (let x in obj) {
            key = x;
            value = obj[x];
            $("[name='" + key + "'],[name='" + key + "[]']", form).each(function () {
                tagName = $(this)[0].tagName;
                type = $(this).attr('type');
                if (tagName == 'INPUT') {
                    if (type == 'radio') {
                        $(this).attr('checked', $(this).val() == value);
                    } else if (type == 'checkbox') {
                        arr = value.split(',');
                        for (var i = 0; i < arr.length; i++) {
                            if ($(this).val() == arr[i]) {
                                $(this).attr('checked', true);
                                break;
                            }
                        }
                    } else {
                        $(this).val(value);
                    }
                } else if (tagName == 'SELECT') {
                    $(this).find("option[value='" + value + "']").attr("selected",true);
                } else if(tagName == 'TEXTAREA') {
                    $(this).val(value);
                }

            });
        }
    } catch (e) {
        alert("加载表单：" + e.message + ",数据内容" + JSON.stringify(jsonStr));
    }
}

export function setFieldData(name, form, value) {
    $("[name='" + name + "'],[name='" + name + "[]']", form).each(function () {
        let tagName = $(this)[0].tagName;
        let type = $(this).attr('type');
        if (tagName == 'INPUT') {
            if (type == 'radio') {
                $(this).attr('checked', $(this).val() == value);
            } else if (type == 'checkbox') {
                arr = value.split(',');
                for (var i = 0; i < arr.length; i++) {
                    if ($(this).val() == arr[i]) {
                        $(this).attr('checked', true);
                        break;
                    }
                }
            } else {
                $(this).val(value);
            }
        } else if (tagName == 'SELECT') {
            $(this).find("option[value='" + value + "']").attr("selected",true);
        } else if(tagName == 'TEXTAREA') {
            $(this).val(value);
        }
    })
}
